<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
      class="fromStyle"
      inline
    >
      <el-form-item label="物业类型" prop="type">
        <ren-select
          v-model="dataForm.dictType"
          dict-type="dictType"
          placeholder="物业类型"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="transactionType">
        <!-- <el-input v-model="dataForm.transactionType" placeholder="交易类型"></el-input> -->
        <ren-select
          v-model="dataForm.transactionType"
          dict-type="houseTransactionType"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="客源类型" prop="customerType">
        <!-- <el-input v-model="dataForm.customerType" placeholder="客源类型"></el-input> -->
        <el-select v-model="dataForm.customerType" style="width:100%">
          <el-option label="写租" :value="1"></el-option>
          <el-option label="写买" :value="2"></el-option>
          <el-option label="住租" :value="3"></el-option>
          <el-option label="住买" :value="4"></el-option>
          <el-option label="商租" :value="5"></el-option>
          <el-option label="商买" :value="6"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="客源id" prop="customerId">
        <el-input v-model="dataForm.customerId" placeholder="客源id"></el-input>
      </el-form-item>
      <el-form-item label="房源id" prop="houseId">
        <el-input v-model="dataForm.houseId" placeholder="房源id"></el-input>
      </el-form-item>
      <el-form-item label="房间id" prop="fjId">
        <el-input v-model="dataForm.fjId" placeholder="房间id"></el-input>
      </el-form-item>-->
      <el-form-item label="合同状态" prop="state">
        <el-input v-model="dataForm.state" placeholder="合同状态"></el-input>
      </el-form-item>
      <el-form-item label="房源面积" prop="area">
        <el-input v-model="dataForm.area" placeholder="房源面积"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="总价" prop="prices">
        <el-input v-model="dataForm.prices" placeholder="总价"></el-input>
      </el-form-item>
      <el-form-item label="价格单位 1元 2万元" prop="unit">
        <el-input v-model="dataForm.unit" placeholder="价格单位 1元 2万元"></el-input>
      </el-form-item>
      <el-form-item label="佣金" prop="commission">
        <el-input v-model="dataForm.commission" placeholder="佣金"></el-input>
      </el-form-item>
      <el-form-item label="房东佣金" prop="ownerCommission">
        <el-input v-model="dataForm.ownerCommission" placeholder="房东佣金"></el-input>
      </el-form-item>
      <el-form-item label="客户佣金" prop="customerCommission">
        <el-input v-model="dataForm.customerCommission" placeholder="客户佣金"></el-input>
      </el-form-item>
      <el-form-item label="是否开票" prop="invoice">
        <el-input v-model="dataForm.invoice" placeholder="是否开票"></el-input>
      </el-form-item>
      <el-form-item label="开票佣金" prop="invoiceCommission">
        <el-input v-model="dataForm.invoiceCommission" placeholder="开票佣金"></el-input>
      </el-form-item>
      <el-form-item label="外佣" prop="externalCommission">
        <el-input v-model="dataForm.externalCommission" placeholder="外佣"></el-input>
      </el-form-item>
      <el-form-item label="平台佣金" prop="platformCommission">
        <el-input v-model="dataForm.platformCommission" placeholder="平台佣金"></el-input>
      </el-form-item>
      <el-form-item label="业绩佣金" prop="achievementCommission">
        <el-input v-model="dataForm.achievementCommission" placeholder="业绩佣金"></el-input>
      </el-form-item>
      <el-form-item label="房源经纪人" prop="houseAgentId">
        <el-input v-model="dataForm.houseAgentId" placeholder="房源经纪人"></el-input>
      </el-form-item>
      <el-form-item label="客源经纪人" prop="customerAgentId">
        <el-input v-model="dataForm.customerAgentId" placeholder="客源经纪人"></el-input>
      </el-form-item>
      <el-form-item label="客户姓名" prop="customerName">
        <el-input v-model="dataForm.customerName" placeholder="客户姓名"></el-input>
      </el-form-item>
      <el-form-item label="客户电话1" prop="customerPhone1">
        <el-input v-model="dataForm.customerPhone1" placeholder="客户电话1"></el-input>
      </el-form-item>
      <el-form-item label="客户电话2" prop="customerPhone2">
        <el-input v-model="dataForm.customerPhone2" placeholder="客户电话2"></el-input>
      </el-form-item>
      <el-form-item label="客户电话3" prop="customerPhone3">
        <el-input v-model="dataForm.customerPhone3" placeholder="客户电话3"></el-input>
      </el-form-item>
      <el-form-item label="业主姓名" prop="ownerName">
        <el-input v-model="dataForm.ownerName" placeholder="业主姓名"></el-input>
      </el-form-item>
      <el-form-item label="业主电话" prop="ownerPhone1">
        <el-input v-model="dataForm.ownerPhone1" placeholder="业主电话"></el-input>
      </el-form-item>
      <el-form-item label="业主电话2" prop="ownerPhone2">
        <el-input v-model="dataForm.ownerPhone2" placeholder="业主电话2"></el-input>
      </el-form-item>
      <el-form-item label="业主电话3" prop="ownerPhone3">
        <el-input v-model="dataForm.ownerPhone3" placeholder="业主电话3"></el-input>
      </el-form-item>
      <el-form-item label="装修情况" prop="renovation">
        <el-input v-model="dataForm.renovation" placeholder="装修情况"></el-input>
      </el-form-item>
      <el-form-item label="家具情况" prop="furniture">
        <el-input v-model="dataForm.furniture" placeholder="家具情况"></el-input>
      </el-form-item>
      <el-form-item label="税收" prop="taxation">
        <el-input v-model="dataForm.taxation" placeholder="税收"></el-input>
      </el-form-item>
      <el-form-item label="合同到期时间" prop="expireDate">
        <el-input v-model="dataForm.expireDate" placeholder="合同到期时间"></el-input>
      </el-form-item>
      <div v-if="dataForm.transactionType==1">
        <el-form-item label="合同租赁月数" prop="tenanTime">
          <el-input v-model="dataForm.tenanTime" placeholder="合同租赁月数"></el-input>
        </el-form-item>
        <el-form-item label="免租天数" prop="tenanFreeTime">
          <el-input v-model="dataForm.tenanFreeTime" placeholder="免租天数"></el-input>
        </el-form-item>
        <el-form-item label="递增" prop="increase">
          <el-input v-model="dataForm.increase" placeholder="递增"></el-input>
        </el-form-item>
        <el-form-item label="付款方式" prop="tenanPay">
          <el-input v-model="dataForm.tenanPay" placeholder="付款方式"></el-input>
        </el-form-item>
      </div>
      <div v-if="dataForm.transactionType==2">
        <el-form-item label="付款方式" prop="payType">
          <el-input v-model="dataForm.payType" placeholder="付款方式"></el-input>
        </el-form-item>
        <el-form-item label="首付" prop="payments">
          <el-input v-model="dataForm.payments" placeholder="首付"></el-input>
        </el-form-item>
        <el-form-item label="贷款金额" prop="loanPrice">
          <el-input v-model="dataForm.loanPrice" placeholder="贷款金额"></el-input>
        </el-form-item>
        <el-form-item label="抵押信息" prop="mortgage">
          <el-input v-model="dataForm.mortgage" placeholder="抵押信息"></el-input>
        </el-form-item>
        <el-form-item label="发票" prop="buyBill">
          <el-input v-model="dataForm.buyBill" placeholder="发票"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="过户费用 元" prop="transferTax">
        <el-input v-model="dataForm.transferTax" placeholder="过户费用 元"></el-input>
      </el-form-item>
      <el-form-item label="预计签约时间" prop="signingData">
        <el-input v-model="dataForm.signingData" placeholder="预计签约时间"></el-input>
      </el-form-item>
      <el-form-item label="过户时间" prop="transferTime">
        <el-input v-model="dataForm.transferTime" placeholder="过户时间"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="审核" prop="examine">
        <el-input v-model="dataForm.examine" placeholder="审核"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        type: "",
        transactionType: "",
        customerType: "",
        customerId: "",
        houseId: "",
        fjId: "",
        state: "",
        area: "",
        price: "",
        prices: "",
        unit: "",
        commission: "",
        ownerCommission: "",
        customerCommission: "",
        invoice: "",
        invoiceCommission: "",
        externalCommission: "",
        platformCommission: "",
        achievementCommission: "",
        houseAgentId: "",
        customerAgentId: "",
        customerName: "",
        customerPhone1: "",
        customerPhone2: "",
        customerPhone3: "",
        ownerName: "",
        ownerPhone1: "",
        ownerPhone2: "",
        ownerPhone3: "",
        renovation: "",
        furniture: "",
        taxation: "",
        tenanTime: "",
        tenanFreeTime: "",
        increase: "",
        tenanPay: "",
        expireDate: "",
        payType: "",
        payments: "",
        loanPrice: "",
        mortgage: "",
        buyBill: "",
        transferTax: "",
        signingData: "",
        transferTime: "",
        remark: "",
        examine: "",
        tenantCode: "",
        deleteFlag: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: ""
      }
    };
  },
  computed: {
    dataRule() {
      return {
        type: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        transactionType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        houseId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        fjId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        state: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        area: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        prices: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        unit: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        commission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        ownerCommission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerCommission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        invoice: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        invoiceCommission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        externalCommission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        platformCommission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        achievementCommission: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        houseAgentId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerAgentId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerPhone1: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerPhone2: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        customerPhone3: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        ownerName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        ownerPhone1: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        ownerPhone2: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        ownerPhone3: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        renovation: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        furniture: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        taxation: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        tenanTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        tenanFreeTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        increase: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        tenanPay: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        expireDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        payType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        payments: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        loanPrice: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        mortgage: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        buyBill: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        transferTax: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        signingData: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        transferTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        remark: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        examine: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        tenantCode: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        deleteFlag: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        updater: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
        updateDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ]
      };
    }
  },
  mounted() {},
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/contract/contractinfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/contract/contractinfo/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
